import time
from DrissionPage import WebPage
from common.utils.web_page_util import create_page, test_page
from common.logger.log import log_
from common.utils.web_page_util import MockTap, Input
from autoTask.operaitonAid.utils.custom_errors import LoginError

# 首页
home_url = 'https://mms.pinduoduo.com/home/'
# 登录页面
login_url = 'https://mms.pinduoduo.com/login/'


class Login:
    def __init__(self, account: str, password: str, page: WebPage = None):
        self.account = account
        self.password = password
        self.page = page

    def execute(self):
        log_.info('开始登录')
        if self.page is None:
            self.page = create_page(home_url)
        is_login = self.check_login()
        if is_login:
            return is_login
        else:
            self.login_action()

    def check_login(self):
        page = self.page
        page.get(home_url)
        store_name = page.ele('x://div[@class="user-name-id"]/span[@class="user-name-text"]')
        if store_name:
            if store_name.text == self.account:
                log_.info('已登陆')
                self.page = page
                return True
            else:
                log_.info('账号不匹配，退出登录，切换账号')
                # MockTap(ele='x://div[@class="user-name"]', msg='error', before_delay=(2, 3))(self.page)
                # MockTap(ele='x://div[@class="logout"]', msg='退出失败', before_delay=(1, 2))(self.page)
                page.get('https://mms.pinduoduo.com/janus/api/logout')
                return False
        else:
            log_.info('未登录，执行登录操作')
            return False

    def login_action(self):
        self.page.get(login_url)
        MockTap(ele='x://div[@class="login-tab"]/div/div[2]', msg='点击账号登录失败', before_delay=(2, 3))(self.page)
        Input(ele='x://input[@id="usernameId"]', vals=self.account, msg='输入账号失败', before_delay=(1, 2))(self.page)
        Input(ele='x://input[@id="passwordId"]', vals=self.password, msg='输入密码失败', before_delay=(1, 2))(self.page)
        MockTap(ele='x://div[@class="info-content"]/button', msg='点击登录失败', before_delay=(1, 2))(self.page)
        time.sleep(1)
        self.check_code()
        self.check_error()
        self.check_login()

    def check_code(self):
        page = self.page
        code_box = page.ele('x://section[@class="verify-phone-container"]//span[@class="info-error-text"]')
        if code_box:
            log_.error('登录失败，需要验证码')
            raise LoginError('登录失败，需要验证码')

    def check_error(self):
        # self.page = test_page()
        page = self.page
        try:
            warn = page.ele('x://div[@class="info-danger-warn"]')
            if warn.text == '':
                pass
            else:
                raise LoginError(f'登录失败，{warn.text}')
        except:
            pass


if __name__ == '__main__':
    # Login('赫巴1舰店数据', 'Yzt123456').execute()
    # Login('赫巴兹旗舰店优', 'xxxxx').check_login()
    # Login('赫巴兹家居官方旗舰店数据1', 'Yzt123456').execute()
    # Login('赫巴兹家居官方旗舰店数据', 'Yzt123456').execute()
    Login('赫巴兹旗舰店优', 'Zty12133').execute()
